<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="WEB应用多页应用应用配置多页应用（MultiPage Application，MPA），指有多个独立的页面的应用，页面之间的跳转需要整页资源刷新。 开启 MPAbuild.json 中通过 mpa 选项开启，目前 web&#x2F;weex&#x2F;kraken 支持 MPA 模式，开启 mpa 之后 src&#x2F;app.tsx 将失效。 123456&amp;#123;  &quot;web&quot;: &amp;#123;    &quot;mpa&quot;: t">
<meta property="og:type" content="article">
<meta property="og:title" content="RaxWEB应用">
<meta property="og:url" content="http://example.com/2021/11/22/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/RaxWEB%E5%BA%94%E7%94%A8/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="WEB应用多页应用应用配置多页应用（MultiPage Application，MPA），指有多个独立的页面的应用，页面之间的跳转需要整页资源刷新。 开启 MPAbuild.json 中通过 mpa 选项开启，目前 web&#x2F;weex&#x2F;kraken 支持 MPA 模式，开启 mpa 之后 src&#x2F;app.tsx 将失效。 123456&amp;#123;  &quot;web&quot;: &amp;#123;    &quot;mpa&quot;: t">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/Rax%E5%BA%94%E7%94%A8/O1CN01p9jhBd1bAEnwhpTPh_!!6000000003424-2-tps-2361-635.png">
<meta property="article:published_time" content="2021-11-22T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-24T08:00:06.416Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="公司技术栈">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/Rax%E5%BA%94%E7%94%A8/O1CN01p9jhBd1bAEnwhpTPh_!!6000000003424-2-tps-2361-635.png">

<link rel="canonical" href="http://example.com/2021/11/22/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/RaxWEB%E5%BA%94%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>RaxWEB应用 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/11/22/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/RaxWEB%E5%BA%94%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          RaxWEB应用
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-11-22 12:18:39" itemprop="dateCreated datePublished" datetime="2021-11-22T12:18:39+08:00">2021-11-22</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-24 16:00:06" itemprop="dateModified" datetime="2021-11-24T16:00:06+08:00">2021-11-24</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">盒马技术栈</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="WEB应用"><a href="#WEB应用" class="headerlink" title="WEB应用"></a>WEB应用</h1><h2 id="多页应用"><a href="#多页应用" class="headerlink" title="多页应用"></a>多页应用</h2><h3 id="应用配置"><a href="#应用配置" class="headerlink" title="应用配置"></a>应用配置</h3><p>多页应用（MultiPage Application，MPA），指有多个独立的页面的应用，页面之间的跳转需要整页资源刷新。</p>
<h4 id="开启-MPA"><a href="#开启-MPA" class="headerlink" title="开启 MPA"></a>开启 MPA</h4><p>build.json 中通过 <code>mpa</code> 选项开启，目前 web/weex/kraken 支持 MPA 模式，开启 mpa 之后 <code>src/app.tsx</code> 将失效。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"web"</span>: &#123;</span><br><span class="line">    <span class="attr">"mpa"</span>: <span class="literal">true</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">"targets"</span>: [<span class="string">"web"</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="配置页面"><a href="#配置页面" class="headerlink" title="配置页面"></a>配置页面</h4><p>在 <code>app.json</code> 通过 <code>routes</code> 字段配置不同页面：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span>,</span><br><span class="line">      <span class="attr">"window"</span>: &#123;</span><br><span class="line">        <span class="attr">"title"</span>: <span class="string">"Home 页面"</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>页面 URL</p>
<p>默认情况下通过页面路径生成产物路径，比如 <code>pages/Home/index</code> 会被构建输出为 <code>build/web/home.html</code>，托管到服务端的地址即为 <code>web.html</code>。如果开发者有特殊需求（比如旧工程版本升级），可以通过 <code>name</code> 字段进行自定义：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"myhome"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"pages/about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如上配置后：</p>
<ul>
<li>Home 页面对应的产物路径为 <code>build/web/myhome.html</code>，托管后的地址即为 <code>/myhome.html</code></li>
<li>Aboue 页面对应的产物路径为 <code>build/web/pages/about.html</code>，托管后的地址即为 <code>/pages/about.html</code></li>
</ul>
<h4 id="window"><a href="#window" class="headerlink" title="window"></a>window</h4><p>可以设置应用的窗口表现，同时也支持针对每个页面设置窗口表现。目前已经支持的参数的有：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">title</td>
<td align="left">页面标题</td>
</tr>
</tbody></table>
<h4 id="tabBar"><a href="#tabBar" class="headerlink" title="tabBar"></a>tabBar</h4><p>配置式 TabBar</p>
<p>框架支持通过简单的配置渲染一个 TabBar 组件：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"home"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"profile"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Profile/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/detail/index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"tabBar"</span>: &#123;</span><br><span class="line">    <span class="attr">"textColor"</span>: <span class="string">"#999"</span>,</span><br><span class="line">    <span class="attr">"selectedColor"</span>: <span class="string">"#666"</span>,</span><br><span class="line">    <span class="attr">"backgroundColor"</span>: <span class="string">"#f8f8f8"</span>,</span><br><span class="line">    <span class="attr">"items"</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">"pageName"</span>: <span class="string">"home"</span>,</span><br><span class="line">        <span class="attr">"icon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1ypSMTcfpK1RjSZFOXXa6nFXa-144-144.png"</span>,</span><br><span class="line">        <span class="attr">"activeIcon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1NBiCTgHqK1RjSZFPXXcwapXa-144-144.png"</span></span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">"pageName"</span>: <span class="string">"about"</span>,</span><br><span class="line">        <span class="attr">"icon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1ypSMTcfpK1RjSZFOXXa6nFXa-144-144.png"</span>,</span><br><span class="line">        <span class="attr">"activeIcon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1NBiCTgHqK1RjSZFPXXcwapXa-144-144.png"</span></span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">"path"</span>: <span class="string">"https://taobao.com"</span>,</span><br><span class="line">        <span class="attr">"pageName"</span>: <span class="string">"profile"</span>,</span><br><span class="line">        <span class="attr">"icon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1ypSMTcfpK1RjSZFOXXa6nFXa-144-144.png"</span>,</span><br><span class="line">        <span class="attr">"activeIcon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1NBiCTgHqK1RjSZFPXXcwapXa-144-144.png"</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>tabBar 配置项如下：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">类型</th>
<th align="left">是否必填</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">textColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">文字颜色</td>
</tr>
<tr>
<td align="left">selectedColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">选中文字颜色</td>
</tr>
<tr>
<td align="left">backgroundColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">背景色</td>
</tr>
<tr>
<td align="left">items</td>
<td align="left">Array</td>
<td align="left">是</td>
<td align="left">每个 tab 配置</td>
</tr>
</tbody></table>
<p>tab item 配置项如下：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">类型</th>
<th align="left">必填</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">text</td>
<td align="left">String</td>
<td align="left">是</td>
<td align="left">tab item 上显示的文本</td>
</tr>
<tr>
<td align="left">pageName</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">显示该 tab 的页面名称</td>
</tr>
<tr>
<td align="left">icon</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">非选中状态图标路径</td>
</tr>
<tr>
<td align="left">activeIcon</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">选中状态图标路径</td>
</tr>
<tr>
<td align="left">path</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">跳转的三方链接（当配置了 path 时，pageName 不再生效）</td>
</tr>
</tbody></table>
<p>自定义 TabBar如下：</p>
<p>框架除了支持配置式 TabBar，还支持自定义 TabBar 组件：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"home"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"name"</span>: <span class="string">"about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/detail/index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"tabBar"</span>: &#123;</span><br><span class="line">    <span class="attr">"custom"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">"list"</span>: [</span><br><span class="line">      <span class="string">"home"</span>,</span><br><span class="line">      <span class="string">"about"</span></span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在开启 <code>custom: true</code> 时，框架约束 <code>src/components/CustomTabBar/index.jsx</code> 导出的组件即为 TabBar 组件，<code>list</code> 字段表示在哪些页面需要显示 TabBar。</p>
<h4 id="调试阶段编译指定页面"><a href="#调试阶段编译指定页面" class="headerlink" title="调试阶段编译指定页面"></a>调试阶段编译指定页面</h4><p>随着多页应用页面变多，编译速度会受到影响，因此 rax-app 支持在开发阶段通过 <code>npm start -- --mpa-entry=home</code> 指定你需要编译的页面，如果存在多个页面可以通过逗号分隔：<code>npm start -- --mpa-entry=home,about</code>。</p>
<h3 id="页面入口"><a href="#页面入口" class="headerlink" title="页面入口"></a>页面入口</h3><p>MPA 场景下支持三种类型的页面入口：</p>
<ol>
<li>推荐导出组件</li>
<li>不推荐 自定义渲染</li>
<li>runApp</li>
</ol>
<h4 id="导出组件"><a href="#导出组件" class="headerlink" title="导出组件"></a>导出组件</h4><p>默认情况下，推荐在页面入口 <code>pages/Home/index.tsx</code> 中直接导出一个组件：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// pages/Home/index</span></span><br><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">Home</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home Page<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Home.getInitialProps = <span class="keyword">async</span> () =&gt; &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>框架会调用 <code>render()</code> 方法渲染该页面组件，同时处理 SSR 相关逻辑。</p>
<h4 id="自定义渲染"><a href="#自定义渲染" class="headerlink" title="自定义渲染"></a>自定义渲染</h4><p>如果开发者希望自己调用 <code>render</code> 方法渲染某个页面，只需要<strong>不使用</strong> <code>export default</code> 导出该页面组件即可：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// pages/Home/index</span></span><br><span class="line"><span class="keyword">import</span> &#123; render, createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> DriverUniversal <span class="keyword">from</span> <span class="string">'driver-universal'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Home</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home Page<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">render(<span class="xml"><span class="tag">&lt;<span class="name">Home</span> /&gt;</span></span>, <span class="built_in">document</span>.body, &#123; <span class="attr">driver</span>: DriverUniversal &#125;);</span><br></pre></td></tr></table></figure>

<p>缺陷：</p>
<ul>
<li>不支持 SSR 场景下通过 <code>Home.getInitialProps</code> 获取初始数据</li>
<li>不支持定义 store 实例后自动包裹 <code>store.Provider</code></li>
</ul>
<h4 id="runApp"><a href="#runApp" class="headerlink" title="runApp"></a>runApp</h4><p>MPA 情况下如果页面功能复杂，希望复用 SPA 模式下 runApp 以及 app.json 相关能力，可以在对应目录下添加相关文件：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">  ├── src/                   </span><br><span class="line">  │   └── pages/            </span><br><span class="line">  │       ├── Home/     		</span><br><span class="line">+ │       │   ├── app.js     # 自定义页面配置入口</span><br><span class="line">+ │       │   ├── app.json   # 页面级别的 app.json 配置</span><br><span class="line">- │       │   └── index.jsx</span><br></pre></td></tr></table></figure>

<p>与标准的 SPA 相比，<code>runApp()</code> 需要手动引入 <code>app.json</code> 配置：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="keyword">import</span> staticConfig <span class="keyword">from</span> <span class="string">'./app.json'</span>;</span><br><span class="line"></span><br><span class="line">runApp(</span><br><span class="line">  &#123;</span><br><span class="line">    app: &#123;</span><br><span class="line">      onShow() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'app show...'</span>);</span><br><span class="line">      &#125;,</span><br><span class="line">      onHide() &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'app hide...'</span>);</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">+  staticConfig</span><br><span class="line">);</span><br></pre></td></tr></table></figure>

<p>接着在 app.json 中配置该页面的路由（即便只有一个也必须配置）：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"./index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>src/app.json</code> 的页面入口需要修改为 <code>app.js</code>：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">     <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">     <span class="string">"source"</span>: <span class="string">"pages/Home/app"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果要使用 store 能力，需要通过 <code>app.addProvider</code> 自行包裹 <code>store.Provider</code>：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/Dashboard/app.tsx</span></span><br><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="keyword">import</span> staticConfig <span class="keyword">from</span> <span class="string">'./app.json'</span>;</span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Provider = store.Provider;</span><br><span class="line"></span><br><span class="line">runApp(&#123;</span><br><span class="line">  app: &#123;</span><br><span class="line">+   addProvider: <span class="function">(<span class="params">&#123; children &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">+     <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">Provider</span>&gt;</span>&#123;children&#125;<span class="tag">&lt;/<span class="name">Provider</span>&gt;</span></span>;</span><br><span class="line">+   &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;, staticConfig);</span><br></pre></td></tr></table></figure>

<p>使用 <code>runApp()</code> 入口的缺陷：</p>
<ul>
<li>即便只有一个页面也会引入单页应用相关的依赖比如 <code>history</code></li>
</ul>
<p><strong>注意：</strong> 在使用 app.js 进行渲染的时候，即使该文件导出了一个组件，框架也不会再调用 <code>render</code> 方法渲染该组件</p>
<h3 id="定制-HTML-结构"><a href="#定制-HTML-结构" class="headerlink" title="定制 HTML 结构"></a>定制 HTML 结构</h3><p>框架提供了两种方式定制 HTML 的内容：</p>
<ol>
<li>推荐 在 <code>src/app.json</code> 中修改部分配置</li>
<li>不推荐 手动创建 <code>src/document/index.jsx</code> 文件</li>
</ol>
<h4 id="默认-HTML-内容"><a href="#默认-HTML-内容" class="headerlink" title="默认 HTML 内容"></a>默认 HTML 内容</h4><p>工程中内置的 <code>HTML</code> 模板结构为:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"root"</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="通过-app-json-定制"><a href="#通过-app-json-定制" class="headerlink" title="通过 app.json 定制"></a>通过 app.json 定制</h4><p>支持的配置项如下：</p>
<ul>
<li><code>metas</code>: 插入到 <code>&lt;head&gt;</code> 标签内</li>
<li><code>links</code>: 插入到业务样式之前</li>
<li><code>scripts</code>: 插入到业务脚本之前</li>
<li><code>window</code>: 配置页面标题</li>
</ul>
<p>具体使用方式如下：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span>,</span><br><span class="line">     <span class="attr">"window"</span>: &#123;</span><br><span class="line">        <span class="attr">"title"</span>: <span class="string">"Home Page"</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span>,</span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="comment">// 插入 meta 标签</span></span><br><span class="line">  <span class="attr">"metas"</span>: [</span><br><span class="line">    <span class="string">"&lt;meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" /&gt;"</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="comment">// 插入 link 标签</span></span><br><span class="line">  <span class="attr">"links"</span>: [</span><br><span class="line">    <span class="string">"&lt;link rel=\"dns-prefetch\" href=\"//g.alicdn.com\" /&gt;"</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="comment">// 插入 script 标签</span></span><br><span class="line">  <span class="attr">"scripts"</span>: [</span><br><span class="line">    <span class="string">"&lt;script defer src=\"xxx/index.js\"&gt;&lt;/script&gt;"</span></span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"window"</span>: &#123;</span><br><span class="line">    <span class="comment">// 设置整个应用的 title，优先级低于每个页面单独设置的 title</span></span><br><span class="line">    <span class="attr">"title"</span>: <span class="string">"Rax App"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>配置式的方案目前仅支持统一定制各个页面的 HTML，对于不同页面的 HTML 有差异的场景我们正在支持中，也欢迎向我们反馈你的诉求。</p>
<h4 id="通过-Document-定制"><a href="#通过-Document-定制" class="headerlink" title="通过 Document 定制"></a>通过 Document 定制</h4><p>在 <code>src/document</code> 目录创建 <code>index.tsx</code> 文件, 可以通过 jsx 的方式定制 HTML 结构：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; Root, Style, Script &#125; <span class="keyword">from</span> <span class="string">'rax-document'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Document</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;html&gt;</span><br><span class="line">      &lt;head&gt;</span><br><span class="line">        &lt;meta charset=<span class="string">"utf-8"</span> /&gt;</span><br><span class="line">        &lt;meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no,viewport-fit=cover"</span>/&gt;</span><br><span class="line">        &#123;<span class="comment">/* props.title 的值是通过 app.json 分析注入的 */</span>&#125;</span><br><span class="line">        &lt;title&gt;&#123;props.title&#125;&lt;<span class="regexp">/title&gt;</span></span><br><span class="line"><span class="regexp">        &lt;Style /</span>&gt;</span><br><span class="line">      &lt;<span class="regexp">/head&gt;</span></span><br><span class="line"><span class="regexp">      &lt;body&gt;</span></span><br><span class="line"><span class="regexp">        &#123;/</span>* root container *<span class="regexp">/&#125;</span></span><br><span class="line"><span class="regexp">        &lt;Root /</span>&gt;</span><br><span class="line">        &lt;Script /&gt;</span><br><span class="line">      &lt;<span class="regexp">/body&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>html&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Document;</span><br></pre></td></tr></table></figure>

<p>组件说明：</p>
<ul>
<li><code>Root</code> 组件对应页面的根节点，<code>id</code> 为 <code>root</code></li>
<li><code>Style</code> 组件对应页面的样式资源，由工程插件自动分析页面依赖产出</li>
<li><code>Script</code> 组件对应页面的 JS 资源，由工程插件自动分析页面依赖产出</li>
</ul>
<p>如果希望为不同页面渲染差异化的 HTML，可以通过 <code>App</code> 组件来实现。<code>App</code> 接收的 <code>children</code> 为一个 <code>function</code>，其入参为项目 <code>app.json</code> 下该路由对应的页面信息。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; Root, Style, Script, App &#125; <span class="keyword">from</span> <span class="string">'rax-document'</span>;</span><br><span class="line"><span class="keyword">import</span> appJSON <span class="keyword">from</span> <span class="string">'../app.json'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Document</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;App config=&#123;appJSON&#125;&gt;</span><br><span class="line">      &#123;</span><br><span class="line">        pageInfo =&gt; &#123;</span><br><span class="line">          <span class="built_in">console</span>.log(<span class="string">'Here is pageInfo'</span>, pageInfo);</span><br><span class="line">          <span class="keyword">return</span> (</span><br><span class="line">            &lt;html&gt;</span><br><span class="line">              &lt;head&gt;</span><br><span class="line">                &lt;Style /&gt;</span><br><span class="line">              &lt;<span class="regexp">/head&gt;</span></span><br><span class="line"><span class="regexp">              &lt;body&gt;</span></span><br><span class="line"><span class="regexp">                &lt;Root /</span>&gt;</span><br><span class="line">              &lt;<span class="regexp">/body&gt;</span></span><br><span class="line"><span class="regexp">            &lt;/</span>html&gt;</span><br><span class="line">          );</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &lt;<span class="regexp">/App&gt;</span></span><br><span class="line"><span class="regexp">  );</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">export default Document;</span></span><br></pre></td></tr></table></figure>

<p>Document 语法限制：</p>
<p>由于 Document 使用的是 JSX 语法，而非普通的 HTML。所以，在 Document 中添加 style 或 script 需要结合 <code>dangerouslySetInnerHTML</code> 的方式，示例如下：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;style dangerouslySetInnerHTML=&#123;&#123;<span class="attr">__html</span>: <span class="string">`</span></span><br><span class="line"><span class="string">  p &#123;</span></span><br><span class="line"><span class="string">    color: red;</span></span><br><span class="line"><span class="string">    font-size: 20px;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">`</span>&#125;&#125;&gt;</span><br><span class="line">&lt;<span class="regexp">/style&gt;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">&lt;script type="text/</span>javascript<span class="string">" dangerouslySetInnerHTML=&#123;&#123;__html: `</span></span><br><span class="line"><span class="string">  console.log("</span>Hello World!<span class="string">")</span></span><br><span class="line"><span class="string">`&#125;&#125;&gt;</span></span><br><span class="line"><span class="string">&lt;/script&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="状态管理"><a href="#状态管理" class="headerlink" title="状态管理"></a>状态管理</h3><p>rax-app 内置了状态管理方案，并在此基础上进一步遵循 <strong>“约定优于配置”</strong> 原则，进行抽象和封装，使得状态管理变得非常容易。<strong>约定大于配置。</strong></p>
<p><strong>注意：</strong> 如果开发者没有依照约定的目录结构创建 <code>store</code>，框架将不会注入任何状态管理相关的能力。</p>
<h4 id="使用指南"><a href="#使用指南" class="headerlink" title="使用指南"></a>使用指南</h4><h5 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h5><p>每个页面的状态位于 <code>src/pages/Dashboard/models/</code> 目录，具体的目录结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">src&#x2F;                       # 源码</span><br><span class="line">└── pages&#x2F;                 # 页面</span><br><span class="line">    ├── Dashboard&#x2F;         # Dashboard 页面</span><br><span class="line">    │   ├── models         # Dashboard 页面状态</span><br><span class="line">    │   |   ├── counter.ts</span><br><span class="line">    │   ├── store.ts       # Dashboard 页面级别的自定义 store</span><br><span class="line">    │   └── index.tsx</span><br><span class="line">    └── Home&#x2F;</span><br></pre></td></tr></table></figure>

<h5 id="定义模型状态"><a href="#定义模型状态" class="headerlink" title="定义模型状态"></a>定义模型状态</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/pages/Dashboard/models/counter.ts</span></span><br><span class="line"><span class="keyword">const</span> delay = <span class="function">(<span class="params">time</span>) =&gt;</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve</span>) =&gt;</span> setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> resolve(), time));</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 定义 model 的初始 state</span></span><br><span class="line">  state: &#123;</span><br><span class="line">    count: <span class="number">0</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 定义改变该模型状态的纯函数</span></span><br><span class="line">  reducers: &#123;</span><br><span class="line">    increment(prevState) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count + <span class="number">1</span> &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">    decrement(prevState) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count - <span class="number">1</span> &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  effects: <span class="function">(<span class="params">dispatch</span>) =&gt;</span> (&#123;</span><br><span class="line">    <span class="keyword">async</span> decrementAsync() &#123;</span><br><span class="line">      <span class="keyword">await</span> delay(<span class="number">10</span>);</span><br><span class="line">      dispatch.counter.decrement();</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;),</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="创建-Store-实例"><a href="#创建-Store-实例" class="headerlink" title="创建 Store 实例"></a>创建 Store 实例</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/pages/Dashboard/store.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="keyword">import</span> counter <span class="keyword">from</span> <span class="string">'./models/counter'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = createStore(&#123; counter &#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<h5 id="View-中使用模型状态"><a href="#View-中使用模型状态" class="headerlink" title="View 中使用模型状态"></a>View 中使用模型状态</h5><p>定义 store 实例之后，框架会将对应的 <code>store.Provider</code> 包裹在页面组件上，因此我们可以直接在页面组件以及子组件里使用状态：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/pages/Dashboard/index.tsx</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span> <span class="title">Dashboard</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 使用 counter 模型</span></span><br><span class="line">  <span class="keyword">const</span> [counterState, counterDispatchers] = store.useModel(<span class="string">'counter'</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;span&gt;&#123;counterState.count&#125;&lt;<span class="regexp">/span&gt;</span></span><br><span class="line"><span class="regexp">      &lt;button onClick=&#123;counterDispatchers.increment&#125;&gt;+&lt;/</span>button&gt;</span><br><span class="line">      &lt;button onClick=&#123;counterDispatchers.decrementAsync&#125;&gt;-&lt;<span class="regexp">/button&gt;</span></span><br><span class="line"><span class="regexp">    &lt;/</span>&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="模型规范"><a href="#模型规范" class="headerlink" title="模型规范"></a>模型规范</h4><h5 id="模型定义"><a href="#模型定义" class="headerlink" title="模型定义"></a>模型定义</h5><p>通常模型定义包括 state、reducers、effects 三部分：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  state: &#123;&#125;,</span><br><span class="line"></span><br><span class="line">  reducers: &#123;&#125;,</span><br><span class="line"></span><br><span class="line">  effects: &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="state"><a href="#state" class="headerlink" title="state"></a>state</h5><p>model 的初始 state</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  state: &#123; <span class="attr">count</span>: <span class="number">0</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="reducers"><a href="#reducers" class="headerlink" title="reducers"></a>reducers</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">reducers: &#123; [string]: <span class="function">(<span class="params">prevState, payload</span>) =&gt;</span> any &#125;</span><br></pre></td></tr></table></figure>

<p>一个改变该模型状态的函数集合。这些方法以模型的上一次 prevState 和一个 payload 作为入参，在方法中使用可变的方式来更新状态。这些方法应该是仅依赖于 prevState 和 payload 参数来计算下一个 nextState 的纯函数。对于有副作用的函数，请使用 effects。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  state: &#123; <span class="attr">count</span>: <span class="number">0</span> &#125;,</span><br><span class="line"></span><br><span class="line">+ reducers: &#123;</span><br><span class="line">+   increment (prevState, payload) &#123;</span><br><span class="line">+     <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count + <span class="number">1</span> &#125;;</span><br><span class="line">+   &#125;,</span><br><span class="line">+   decrement (prevState) &#123;</span><br><span class="line">+     <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count - <span class="number">1</span> &#125;;</span><br><span class="line">+   &#125;</span><br><span class="line">+ &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="effects"><a href="#effects" class="headerlink" title="effects"></a>effects</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">effects: <span class="function">(<span class="params">dispatch</span>) =&gt;</span> <span class="function">(<span class="params">&#123; [string]: (payload, rootState</span>) =&gt;</span> <span class="keyword">void</span> &#125;)</span><br></pre></td></tr></table></figure>

<p>一个可以处理该模型副作用的函数集合。这些方法以 payload 和 rootState 作为入参，适用于进行异步调用、模型联动等场景。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  state: &#123; <span class="attr">count</span>: <span class="number">0</span> &#125;,</span><br><span class="line"></span><br><span class="line">  reducers: &#123;</span><br><span class="line">    increment (prevState, payload) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count + <span class="number">1</span> &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">    decrement (prevState) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; <span class="attr">count</span>: prevState.count - <span class="number">1</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">+ effects: <span class="function">(<span class="params">dispatch</span>) =&gt;</span> (&#123;</span><br><span class="line">+   <span class="keyword">async</span> asyncDecrement() &#123;</span><br><span class="line">+     <span class="keyword">await</span> delay(<span class="number">1000</span>);             <span class="comment">// 进行一些异步操作</span></span><br><span class="line">+     dispatch.counter.increment();  <span class="comment">// 调用模型 reducers 内的方法来更新状态</span></span><br><span class="line">+   &#125;,</span><br><span class="line">+ &#125;),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>在 effects 中的 action 方法中可以通过 <code>dispatch[model][action]</code> 拿到其他模型所定义的方法。</p>
<h5 id="模型使用"><a href="#模型使用" class="headerlink" title="模型使用"></a>模型使用</h5><p>定义好 model 之后，我们就可以通过 <code>useModel</code> 传入对应的 model 名称进行使用，并返回该模型的状态（state）和调度器（dispatchers）。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">useModel(name: string): [ state, dispatchers ]</span><br><span class="line">+ <span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'./store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; &#123;</span><br><span class="line">+ <span class="keyword">const</span> [counterState, counterDispatchers] = store.useModel(<span class="string">'counter'</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;&#123;counterState.count&#125;&lt;<span class="regexp">/&gt;</span></span><br><span class="line"><span class="regexp">  )</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="API"><a href="#API" class="headerlink" title="API"></a>API</h4><p>createStore</p>
<p>该方法用于创建 Store。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">createStore(models, options);</span><br></pre></td></tr></table></figure>

<p><strong>options</strong></p>
<ul>
<li>disableImmer: 布尔值, 可选, 默认值 false，如果将此设置为true，那么 immer 将被禁用，这意味着您不能再在 reducers 中直接改变状态，而是必须返回新的状态。</li>
<li>disableError: 布尔值, 可选, 默认值 false，如果将此设置为 true，则 <code>UseModelEffectsError</code> 和 <code>WithModelEffectsError</code> 将不可用，仅当您非常关注性能或故意抛出错误时才启用该选项。</li>
<li>disableLoading: 布尔值, 可选, 默认值 false，如果将此设置为 true，则 <code>useModelEffectsLoading</code> 和 <code>withModelEffectsLoading</code> 将不可用。</li>
</ul>
<p>useModelState</p>
<p>通过该 hooks 使用模型的状态并订阅其更新。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">FunctionComponent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> state = store.useModelState(<span class="string">'counter'</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(state.value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>useModelDispatchers</p>
<p>仅使用 Action 不使用 State。有些时候组件中只需要触发 action 不需要依赖对应的数据状态，此时可以使用 <code>useModelDispatchers</code> API。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">FunctionComponent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> dispatchers = store.useModelDispatchers(<span class="string">'counter'</span>);</span><br><span class="line">  <span class="comment">// 只调用 increment 方法</span></span><br><span class="line">  dispatchers.increment();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>useModelEffectsState</p>
<p>异步 Action 状态。通过 <code>useModelEffectsState</code> API 即可获取到异步请求的 loading 和 error 状态。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">FunctionComponent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> [state, dispatchers] = store.useModel(<span class="string">'counter'</span>);</span><br><span class="line">  <span class="keyword">const</span> effectsState = store.useModelEffectsState(<span class="string">'counter'</span>);</span><br><span class="line"></span><br><span class="line">  useEffect(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    dispatchers.asyncDecrement();</span><br><span class="line">  &#125;, []);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// loading</span></span><br><span class="line">  <span class="built_in">console</span>.log(effectsState.asyncDecrement.isLoading);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// error</span></span><br><span class="line">  <span class="built_in">console</span>.log(effectsState.asyncDecrement.error);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>withModel</p>
<p>在 Class Component 中使用。useModel 相关的 API 基于 React 的 Hooks 能力，仅能在 Function Component 中使用，通过 <code>withModel</code> API 可以实现在 Class Component 中使用。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TodoList</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span> </span>&#123;</span><br><span class="line">  render() &#123;</span><br><span class="line">    <span class="keyword">const</span> &#123; todos &#125; = <span class="keyword">this</span>.props;</span><br><span class="line">    <span class="keyword">const</span> [ state, dispatchers ] = todos;</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store.withModel(<span class="string">'todos'</span>)(TodoList);</span><br><span class="line"><span class="comment">// 绑定多个 model</span></span><br><span class="line"><span class="comment">// export default withModel('user')(withModel('todos')(TodoList));</span></span><br></pre></td></tr></table></figure>

<p>同时，也可以使用 <code>withModelDispatchers</code> 以及 <code>withModelEffectsState</code> API。</p>
<h4 id="类型提示"><a href="#类型提示" class="headerlink" title="类型提示"></a>类型提示</h4><p>编写类型有助于更好的代码提示，类型定义步骤如下：</p>
<h5 id="创建-Store-实例时"><a href="#创建-Store-实例时" class="headerlink" title="创建 Store 实例时"></a>创建 Store 实例时</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/pages/Dashboard/store.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore, IStoreModels, IStoreDispatch, IStoreRootState &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="keyword">import</span> counter <span class="keyword">from</span> <span class="string">'./models/counter'</span>;</span><br><span class="line"></span><br><span class="line">+interface IAppStoreModels extends IStoreModels &#123;</span><br><span class="line">+  counter: <span class="keyword">typeof</span> counter;</span><br><span class="line">+&#125;;</span><br><span class="line"></span><br><span class="line">+<span class="keyword">const</span> models: IAppStoreModels = &#123;</span><br><span class="line">+  counter</span><br><span class="line">+&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> createStore(models);</span><br><span class="line"></span><br><span class="line">+<span class="comment">// 导出 IRootDispatch 类型</span></span><br><span class="line">+<span class="keyword">export</span> type IRootDispatch = IStoreDispatch&lt;<span class="keyword">typeof</span> models&gt;;</span><br><span class="line"></span><br><span class="line">+<span class="comment">// 导出 IRootState 类型</span></span><br><span class="line">+<span class="keyword">export</span> type IRootState = IStoreRootState&lt;<span class="keyword">typeof</span> models&gt;;</span><br></pre></td></tr></table></figure>

<h5 id="定义-Model-时"><a href="#定义-Model-时" class="headerlink" title="定义 Model 时"></a>定义 Model 时</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/pages/Dashboard/models/counter.ts</span></span><br><span class="line">+<span class="keyword">import</span> &#123; IRootState, IRootDispatch &#125; <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  state: [],</span><br><span class="line">  reducers: &#123;&#125;,</span><br><span class="line">+ effects: (dispatch: <span class="function"><span class="params">IRootDispatch</span> =&gt;</span> (&#123;</span><br><span class="line">+   like(playload, <span class="attr">rootState</span>: IRootState) &#123;</span><br><span class="line"></span><br><span class="line">+   &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="静态构建"><a href="#静态构建" class="headerlink" title="静态构建"></a>静态构建</h3><p>静态构建是指在构建过程中，将页面内容提前构建到 HTML 里，静态构建与 SSR 的执行流程类似，只是将 SSR 的渲染时机提前到静态构建时。</p>
<p>哪些场景下适合静态构建</p>
<p>常见的不依赖于动态数据的页面，比如：官网、产品介绍页、规则说明页等等，这类静态页面都适合使用静态构建来避免不必要的 CSR 渲染过程。</p>
<p>对于普通的页面，如果页面不是完全依赖动态数据产生（比如商品列表），可以将页面的框架结构提前构建为 HTML 从而提升页面访问的体感。</p>
<p>如何开启静态构建</p>
<p>在 <code>build.json</code> 中，设置 <code>staticExport</code> 为 <code>true</code> 以开启静态构建能力。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"targets"</span>: [<span class="string">"web"</span>],</span><br><span class="line">  <span class="attr">"web"</span>: &#123;</span><br><span class="line">    <span class="attr">"mpa"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">"staticExport"</span>: <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="快照"><a href="#快照" class="headerlink" title="快照"></a>快照</h3><p>在传统的 CSR 渲染链路中，开发者的 js bundle 未执行前页面表现为白屏。在弱网条件下，白屏时间过长会导致用户体验下降。快照功能会将 HTML 内容缓存到 localStorage 中，从而在用户二次访问时极速展示页面内容，提升<strong>二次访问</strong>时的首屏加载速度。</p>
<p>在 <code>build.json</code> 中一键开启：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"targets"</span>: [<span class="string">"web"</span>],</span><br><span class="line">  <span class="attr">"web"</span>: &#123;</span><br><span class="line">    <span class="attr">"mpa"</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">"snapshot"</span>: <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="单页应用"><a href="#单页应用" class="headerlink" title="单页应用"></a>单页应用</h2><h3 id="应用入口"><a href="#应用入口" class="headerlink" title="应用入口"></a>应用入口</h3><p>对于 SPA 应用或者小程序应用，<code>src/app.tsx</code> 即整个应用的入口，通过 <code>runApp()</code> 运行应用：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line">runApp(&#123;&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="配置项"><a href="#配置项" class="headerlink" title="配置项"></a>配置项</h4><p><code>runApp()</code> 接受一个对象作为配置参数，完整的配置参数如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  app: &#123;</span><br><span class="line">    <span class="comment">// 可选，自定义添加 Provider</span></span><br><span class="line">    addProvider: <span class="function">(<span class="params">&#123; children &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">ConfigProvider</span>&gt;</span>&#123;children&#125;<span class="tag">&lt;/<span class="name">ConfigProvider</span>&gt;</span></span>;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可选，常用于 SSR 场景或者异步获取数据请求的场景</span></span><br><span class="line">    <span class="comment">// 如果返回字段中包含 initialStates 字段将会作为状态管理 store 的初始值</span></span><br><span class="line">    getInitialData: <span class="keyword">async</span>() =&gt; &#123;</span><br><span class="line">      <span class="keyword">const</span> result = <span class="keyword">await</span> fetch();</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 可选，开启默认的 ErrorBoundary 行为，默认值为 false</span></span><br><span class="line">    errorBoundary: <span class="literal">true</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可选，自定义错误边界的 fallback UI</span></span><br><span class="line">    ErrorBoundaryFallback: <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>渲染错误<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可选，自定义错误的处理事件</span></span><br><span class="line">    onErrorBoundaryHandler: (error, componentStack) &#123;</span><br><span class="line">      <span class="comment">// Do something with the error</span></span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可选，应用生命周期</span></span><br><span class="line">    onShow() &#123;&#125;,</span><br><span class="line">    onLaunch() &#123;&#125;,</span><br><span class="line">    onHide() &#123;&#125;,</span><br><span class="line">    onError() &#123;&#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  router: &#123;</span><br><span class="line">    <span class="comment">// 默认值 hash，可选值 browser|hash|static</span></span><br><span class="line">    type: <span class="string">'browser'</span>,</span><br><span class="line">    <span class="comment">// 路由基准地址</span></span><br><span class="line">    basename: <span class="string">'/seller'</span>,</span><br><span class="line">    <span class="comment">// 加载页面 bundle 时的 fallback UI</span></span><br><span class="line">    fallback: <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>loading...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>,</span><br><span class="line">    <span class="comment">// 自定义创建 history 对象，详见 https://github.com/ReactTraining/history/blob/master/</span></span><br><span class="line">    <span class="comment">// history</span></span><br><span class="line">  &#125;,</span><br><span class="line">  store: &#123;</span><br><span class="line">    initialStates: &#123;&#125;,</span><br><span class="line">    getInitialStates: <span class="function">(<span class="params">initialData</span>) =&gt;</span> &#123;&#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>当在 <code>app.ts</code> 中包含 <code>JSX</code> 元素的时候，需要将 <code>app.ts</code> 更名为 <code>app.tsx</code>。</p>
</blockquote>
<p>getInitialData:</p>
<p>通过 <code>app.getInitialData()</code> 可以在应用启动前获取一些异步状态，比如登录态判断、用户信息获取等。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">+  app: &#123;</span><br><span class="line">+    getInitialData: <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">+      <span class="comment">// const data = await fetch('/api/data');</span></span><br><span class="line">+      <span class="keyword">return</span> &#123; <span class="attr">userId</span>: <span class="string">'123'</span> &#125;;</span><br><span class="line">+    &#125;</span><br><span class="line">+  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<p>对于获取到的初始数据，可以通过 <code>getInitialData</code> API 在任何地方消费：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line">+<span class="keyword">import</span> &#123; getInitialData &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">+  <span class="keyword">const</span> initialData = getInitialData();</span><br><span class="line">  <span class="built_in">console</span>.log(initialData.userId);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="路由方案"><a href="#路由方案" class="headerlink" title="路由方案"></a>路由方案</h3><h4 id="路由配置"><a href="#路由配置" class="headerlink" title="路由配置"></a>路由配置</h4><p>在 <code>src/app.tsx</code> 中，我们可以配置路由的类型和基础路径等路由信息，具体配置如下：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  router: &#123;</span><br><span class="line">    type: <span class="string">'browser'</span>,</span><br><span class="line">    basename: <span class="string">'/seller'</span>,</span><br><span class="line">    fallback: <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>loading...<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<h4 id="页面路由"><a href="#页面路由" class="headerlink" title="页面路由"></a>页面路由</h4><p>对于单页应用，通过 <code>src/app.json</code> 中的 <code>routes</code> 用于指定应用的页面：</p>
<ul>
<li><code>path</code>: 指定页面对应的路由地址</li>
<li><code>source</code>: 指定页面组件地址，必须写成 <code>pages/[PAGE_NAME]/index</code> 格式，暂不支持嵌套式路由</li>
<li><code>targets</code> 指定页面需要构建的端，默认为 <code>build.json</code> 所配置的 <code>targets</code> 的值</li>
<li><code>window</code>: 指定该页面的窗体表现，可以覆盖全局的窗口设置</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [                    </span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span>,</span><br><span class="line">      <span class="attr">"targets"</span>: [<span class="string">"web"</span>]</span><br><span class="line">    &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>框架默认开启路由切割，每个页面会打包出一个独立 bundle</p>
</blockquote>
<h4 id="window-1"><a href="#window-1" class="headerlink" title="window"></a>window</h4><p>可以设置应用的窗口表现，同时也支持针对每个页面设置窗口表现。目前已经支持的参数的有：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">title</td>
<td align="left">页面标题</td>
</tr>
</tbody></table>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [                    </span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span>,</span><br><span class="line">      <span class="attr">"window"</span>: &#123;</span><br><span class="line">        <span class="attr">"title"</span>: <span class="string">"关于 Rax"</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"window"</span>: &#123;</span><br><span class="line">    <span class="attr">"title"</span>: <span class="string">"应用默认 title"</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="tabBar-1"><a href="#tabBar-1" class="headerlink" title="tabBar"></a>tabBar</h4><p>如果你的应用是一个多 tab 应用（底部栏可以切换页面），那么可以通过 <code>tabBar</code> 配置项指定 tab 栏的表现，以及 tab 切换时显示的对应页面。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span></span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">  <span class="attr">"tabBar"</span>: &#123;</span><br><span class="line">    <span class="attr">"textColor"</span>: <span class="string">"#999"</span>,</span><br><span class="line">    <span class="attr">"selectedColor"</span>: <span class="string">"#666"</span>,</span><br><span class="line">    <span class="attr">"backgroundColor"</span>: <span class="string">"#f8f8f8"</span>,</span><br><span class="line">    <span class="attr">"items"</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">"text"</span>: <span class="string">"home"</span>,</span><br><span class="line">        <span class="attr">"pageName"</span>: <span class="string">"/"</span>,</span><br><span class="line">        <span class="attr">"icon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1ypSMTcfpK1RjSZFOXXa6nFXa-144-144.png"</span>,</span><br><span class="line">        <span class="attr">"activeIcon"</span>: <span class="string">"https://gw.alicdn.com/tfs/TB1NBiCTgHqK1RjSZFPXXcwapXa-144-144.png"</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>tabBar 配置项如下：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">类型</th>
<th align="left">是否必填</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">textColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">文字颜色</td>
</tr>
<tr>
<td align="left">selectedColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">选中文字颜色</td>
</tr>
<tr>
<td align="left">backgroundColor</td>
<td align="left">HexColor</td>
<td align="left">否</td>
<td align="left">背景色</td>
</tr>
<tr>
<td align="left">items</td>
<td align="left">Array</td>
<td align="left">是</td>
<td align="left">每个 tab 配置</td>
</tr>
</tbody></table>
<p>tab item 配置项如下：</p>
<table>
<thead>
<tr>
<th align="left">属性</th>
<th align="left">类型</th>
<th align="left">必填</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">pageName</td>
<td align="left">String</td>
<td align="left">是</td>
<td align="left">设置页面路径，值为 <code>routes</code> 中配置的路由</td>
</tr>
<tr>
<td align="left">text</td>
<td align="left">String</td>
<td align="left">是</td>
<td align="left">tab item 上显示的文本</td>
</tr>
<tr>
<td align="left">icon</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">非选中状态图标路径</td>
</tr>
<tr>
<td align="left">activeIcon</td>
<td align="left">String</td>
<td align="left">否</td>
<td align="left">选中状态图标路径</td>
</tr>
</tbody></table>
<h4 id="路由跳转"><a href="#路由跳转" class="headerlink" title="路由跳转"></a>路由跳转</h4><p>可以通过 <code>history</code> 获取路由实例。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; history &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于获取 history 跳转的动作，包含 PUSH、REPLACE 和 POP 三种类型</span></span><br><span class="line"><span class="built_in">console</span>.log(history.action);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于获取 location 对象，包含 pathname、search 和 hash</span></span><br><span class="line"><span class="built_in">console</span>.log(history.location);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于路由跳转</span></span><br><span class="line">history.push(<span class="string">'/home'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于路由替换</span></span><br><span class="line">history.replace(<span class="string">'/home'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 用于跳转到上一个路由</span></span><br><span class="line">history.goBack();</span><br></pre></td></tr></table></figure>

<p>更多 <a href="https://github.com/ReactTraining/history/blob/master/docs/GettingStarted.md" target="_blank" rel="noopener">history API</a></p>
<h4 id="路由参数获取"><a href="#路由参数获取" class="headerlink" title="路由参数获取"></a>路由参数获取</h4><p>可以通过 <code>getSearchParams</code> 获取路由参数。假设当前 URL 为 <code>https://example.com?foo=bar</code>，解析查询参数如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/components/Example</span></span><br><span class="line"><span class="keyword">import</span> &#123; getSearchParams &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Example</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">const</span> searchParams = getSearchParams()</span><br><span class="line">  <span class="comment">// console.log(searchParams); =&gt; &#123; foo: 'bar' &#125;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="常见问题"><a href="#常见问题" class="headerlink" title="常见问题"></a>常见问题</h4><p>HashHistory 与 BrowserHistory</p>
<p>前端路由通常有两种实现方式：HashHistory 和 BrowserHistory，路由都带着 <code>#</code> 说明使用的是 HashHistory。这两种方式优缺点：</p>
<table>
<thead>
<tr>
<th align="left">特点\方案</th>
<th align="left">HashRouter</th>
<th align="left">BrowserRouter</th>
</tr>
</thead>
<tbody><tr>
<td align="left">美观度</td>
<td align="left">不好，有 # 号</td>
<td align="left">好</td>
</tr>
<tr>
<td align="left">易用性</td>
<td align="left">简单</td>
<td align="left">中等，需要 server 配合</td>
</tr>
<tr>
<td align="left">依赖 server</td>
<td align="left">不依赖</td>
<td align="left">依赖</td>
</tr>
<tr>
<td align="left">跟锚点功能冲突</td>
<td align="left">冲突</td>
<td align="left">不冲突</td>
</tr>
<tr>
<td align="left">兼容性</td>
<td align="left">IE8</td>
<td align="left">IE10</td>
</tr>
</tbody></table>
<p>开发者可以根据自己的实际情况选择对应方案。</p>
<p>如何使用 BrowserRouter</p>
<p>本地开发时，只需要在 <code>src/app.tsx</code> 中增加以下配置即可：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  router: &#123;</span><br><span class="line">+   type: <span class="string">'browser'</span>,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<p>线上运行时需要服务端支持，否则会出现刷新 404 问题，具体方案请参考社区文档：</p>
<h3 id="定制-HTML-结构-1"><a href="#定制-HTML-结构-1" class="headerlink" title="定制 HTML 结构"></a>定制 HTML 结构</h3><p>框架提供了两种方式帮助开发者定制 HTML 内容：</p>
<ol>
<li>推荐 通过 <code>app.json</code> 静态配置</li>
<li>不推荐 通过 <code>document/index.jsx</code> 灵活编写</li>
</ol>
<p>以下是两种方案的使用方式。</p>
<p>通过 app.json 定制</p>
<p>支持的配置项如下：</p>
<ul>
<li><code>metas</code>: 插入到 <code>&lt;head&gt;</code> 标签内</li>
<li><code>links</code>: 插入到业务样式之前</li>
<li><code>scripts</code>: 插入到业务脚本之前</li>
<li><code>window</code>: 配置页面标题</li>
</ul>
<p>具体使用方式如下：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"routes"</span>: [</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/Home/index"</span>,</span><br><span class="line">+      "window": &#123;</span><br><span class="line">+        "title": "Home Page"</span><br><span class="line">+      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">"path"</span>: <span class="string">"/about"</span>,</span><br><span class="line">      <span class="attr">"source"</span>: <span class="string">"pages/About/index"</span>,</span><br><span class="line">    &#125;</span><br><span class="line">  ],</span><br><span class="line">+  // 插入 meta 标签</span><br><span class="line">+  "metas": [</span><br><span class="line">+    "&lt;meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" /&gt;"</span><br><span class="line">+  ],</span><br><span class="line">+  // 插入 link 标签</span><br><span class="line">+  "links": [</span><br><span class="line">+    "&lt;link rel=\"dns-prefetch\" href=\"//g.alicdn.com\" /&gt;"</span><br><span class="line">+  ],</span><br><span class="line">+  // 插入 script 标签</span><br><span class="line">+  "scripts": [</span><br><span class="line">+    "&lt;script defer src=\"xxx/index.js\"&gt;&lt;/script&gt;"</span><br><span class="line">+  ],</span><br><span class="line">+  "window": &#123;</span><br><span class="line">+    // 设置整个应用的 title，优先级低于每个页面单独设置的 title</span><br><span class="line">+    "title": "Rax App"</span><br><span class="line">+  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过 Document 定制</p>
<p>同 MPA 文档定制。</p>
<h3 id="状态管理-1"><a href="#状态管理-1" class="headerlink" title="状态管理"></a>状态管理</h3><p>rax-app 内置了状态管理方案，并在此基础上进一步遵循 <strong>“约定优于配置”</strong> 原则，进行抽象和封装，使得状态管理变得非常容易。</p>
<p><strong>注意：</strong> 如果开发者没有依照约定的目录结构创建 <code>store</code>，框架将不会注入任何状态管理相关的能力。</p>
<h4 id="全局状态"><a href="#全局状态" class="headerlink" title="全局状态"></a>全局状态</h4><p>约定全局状态位于 <code>src/models</code> 目录，目录结构如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">src</span><br><span class="line">├── models               &#x2F;&#x2F; 全局状态</span><br><span class="line">|   ├── counter.ts</span><br><span class="line">│   └── user.ts</span><br><span class="line">├── app.tsx </span><br><span class="line">└── store.ts</span><br></pre></td></tr></table></figure>

<h4 id="定义模型"><a href="#定义模型" class="headerlink" title="定义模型"></a>定义模型</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/models/user.ts</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> delay = <span class="function">(<span class="params">time</span>) =&gt;</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve</span>) =&gt;</span> setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> resolve(), time));</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 定义 model 的初始 state</span></span><br><span class="line">  state: &#123;</span><br><span class="line">    name: <span class="string">''</span>,</span><br><span class="line">    id: <span class="string">''</span></span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 定义改变该模型状态的纯函数</span></span><br><span class="line">  reducers: &#123;</span><br><span class="line">    update (prevState, payload) &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...prevState,</span><br><span class="line">        ...payload,</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 定义处理该模型副作用的函数</span></span><br><span class="line">  effects: <span class="function">(<span class="params">dispatch</span>) =&gt;</span> (&#123;</span><br><span class="line">    <span class="keyword">async</span> updateUserInfo () &#123;</span><br><span class="line">      <span class="keyword">await</span> delay(<span class="number">1000</span>);</span><br><span class="line">      dispatch.user.update(&#123;</span><br><span class="line">        name: <span class="string">'taobao'</span>,</span><br><span class="line">        id: <span class="string">'123'</span>,</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;),</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h4 id="创建-Store-实例-1"><a href="#创建-Store-实例-1" class="headerlink" title="创建 Store 实例"></a>创建 Store 实例</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// src/store.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; createStore &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="keyword">import</span> user <span class="keyword">from</span> <span class="string">'./models/user'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> store = createStore(&#123; user &#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> store;</span><br></pre></td></tr></table></figure>

<h4 id="在-View-中使用状态"><a href="#在-View-中使用状态" class="headerlink" title="在 View 中使用状态"></a>在 View 中使用状态</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 引用全局状态</span></span><br><span class="line"><span class="keyword">import</span> store <span class="keyword">from</span> <span class="string">'@/store'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> HomePage = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> [userState, userDispatchers] = store.useModel(<span class="string">'user'</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (</span><br><span class="line">    &lt;&gt;</span><br><span class="line">      &lt;span&gt;&#123;userState.id&#125;&lt;<span class="regexp">/span&gt;</span></span><br><span class="line"><span class="regexp">      &lt;span&gt;&#123;userState.name&#125;&lt;/</span>span&gt;</span><br><span class="line">    &lt;<span class="regexp">/&gt;</span></span><br><span class="line"><span class="regexp">  );</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="模型规范-1"><a href="#模型规范-1" class="headerlink" title="模型规范"></a>模型规范</h4><p>参考 MPA 状态管理</p>
<h4 id="API-1"><a href="#API-1" class="headerlink" title="API"></a>API</h4><p>参考 MPA 状态管理</p>
<h4 id="类型提示-1"><a href="#类型提示-1" class="headerlink" title="类型提示"></a>类型提示</h4><p>参考 MPA 状态管理</p>
<h4 id="设置初始状态"><a href="#设置初始状态" class="headerlink" title="设置初始状态"></a>设置初始状态</h4><p>通过全局的 <code>store.initialStates</code> 即可同步设置整个应用的全局状态：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  app: &#123;&#125;,</span><br><span class="line">  store: &#123;</span><br><span class="line">+    initialStates: &#123;</span><br><span class="line">+      <span class="comment">// 这里的 key 即对应 model 名称</span></span><br><span class="line">+      user: &#123;</span><br><span class="line">+        id: <span class="string">'123'</span></span><br><span class="line">+      &#125;</span><br><span class="line">+    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<p>但是大多数情况下我们都需要异步设置全局状态，此时通过 <code>app.getInitialData()</code> 方法返回 <code>initialStates</code> 即可：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  app: &#123;</span><br><span class="line">    getInitialData: <span class="keyword">async</span>() =&gt; &#123;</span><br><span class="line">      <span class="keyword">const</span> result = <span class="keyword">await</span> fetch();</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">+        initialStates: result.states</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<p>框架约定将 <code>app.getInitialData()</code> 返回的 <code>initialStates</code> 字段作为 store 的初始状态。如果不想使用该约定，可以通过 <code>store.getInitialStates</code> 更灵活的指定初始状态：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  app: &#123;</span><br><span class="line">    getInitialData: <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; <span class="attr">userId</span>: <span class="string">'123'</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  store: &#123;</span><br><span class="line">+   <span class="comment">// 入参 initialData 即 getInitialData() 返回的数据</span></span><br><span class="line">+   getInitialStates: <span class="function">(<span class="params">initialData</span>) =&gt;</span> &#123;</span><br><span class="line">+     <span class="keyword">return</span> &#123;</span><br><span class="line">+       user: &#123; <span class="attr">id</span>: initialData.userId &#125;</span><br><span class="line">+     &#125;;</span><br><span class="line">+   &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="生命周期"><a href="#生命周期" class="headerlink" title="生命周期"></a>生命周期</h3><h4 id="应用生命周期"><a href="#应用生命周期" class="headerlink" title="应用生命周期"></a>应用生命周期</h4><table>
<thead>
<tr>
<th align="left"></th>
<th>行为表现</th>
</tr>
</thead>
<tbody><tr>
<td align="left">onShow</td>
<td>1. 应用唤起之后，在框架 loading 结束后触发 2. 应用从后台切到前台的时候触发</td>
</tr>
<tr>
<td align="left">onHide</td>
<td>应用从前台切到后台的时候触发</td>
</tr>
<tr>
<td align="left">onLaunch</td>
<td>1. 应用启动的时候触发 2. 传入参数为启动参数 (仅对小程序有效)</td>
</tr>
<tr>
<td align="left">onError</td>
<td>1. 监听全局 JS 报错 2. 传入参数为报错信息</td>
</tr>
</tbody></table>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">  app: &#123;</span><br><span class="line">    onShow() &#123;&#125;,</span><br><span class="line">    onLaunch() &#123;&#125;,</span><br><span class="line">    onHide() &#123;&#125;,</span><br><span class="line">    onError() &#123;&#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<h4 id="页面生命周期"><a href="#页面生命周期" class="headerlink" title="页面生命周期"></a>页面生命周期</h4><table>
<thead>
<tr>
<th align="left"></th>
<th align="left">行为表现</th>
</tr>
</thead>
<tbody><tr>
<td align="left">onShow</td>
<td align="left">1. 切到目标页面时触发 2. 应用从后台切到前台的时候触发</td>
</tr>
<tr>
<td align="left">onHide</td>
<td align="left">1. 页面切走时触发 2. 页面卸载的时候触发</td>
</tr>
</tbody></table>
<h4 id="Hooks"><a href="#Hooks" class="headerlink" title="Hooks"></a>Hooks</h4><p>usePageShow</p>
<p>页面切换到前台且页面组件渲染后触发。</p>
<p>usePageHide</p>
<p>页面切换到后台后触发。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; usePageShow &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Home</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  usePageShow(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 首次调用，回调函数会在组件完成渲染后执行</span></span><br><span class="line">  &#125;);</span><br><span class="line">  usePageHide(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    </span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Class Component</p>
<p>使用 <code>withPageLifeCyle</code> 给 Class Component 注入 <code>onShow</code>/<code>onHide</code> 属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement, Component &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; withPageLifeCycle &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Home</span> <span class="keyword">extends</span> <span class="title">Component</span> </span>&#123;</span><br><span class="line">  onShow() &#123;</span><br><span class="line">    <span class="comment">// 首次调用，函数在组件 constructor 之后调用</span></span><br><span class="line">  &#125;</span><br><span class="line">  render() &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> withPageLifeCycle(Home);</span><br></pre></td></tr></table></figure>

<h4 id="触发时序"><a href="#触发时序" class="headerlink" title="触发时序"></a>触发时序</h4><p><strong>启动阶段：</strong> app lanuch =&gt; app show =&gt; page show =&gt; component did mount <strong>页面切换(A =&gt; B)：</strong> page A hide =&gt; page B show <strong>页面返回(B =&gt; A)：</strong> page B hide =&gt; page A show <strong>应用前台切换到后台(A 页面)：</strong> page A hide =&gt; app hide <strong>应用后台切换到前台(A 页面)：</strong> app show =&gt; page A show</p>
<p>服务端渲染</p>
<p>框架支持服务端渲染（即 SSR）能力，开发者可以按需一键开启 SSR 的模式。</p>
<h3 id="开启-SSR"><a href="#开启-SSR" class="headerlink" title="开启 SSR"></a>开启 SSR</h3><p>在工程配置文件 <code>build.json</code> 中开启 SSR：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"web"</span>: &#123;</span><br><span class="line">    <span class="attr">"ssr"</span>: <span class="literal">true</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>配置完之后即可启用 SSR，同理置为 false 即可关闭 SSR 功能。开启后通过命令行 <code>npm run start</code> 即可启动调试服务，此时通过查看源代码发现返回的 HTML 已经是服务端渲染的节点结构：</p>
<p><img src="/Rax%E5%BA%94%E7%94%A8/O1CN01p9jhBd1bAEnwhpTPh_!!6000000003424-2-tps-2361-635.png" alt="SSR 效果"></p>
<h4 id="应用级数据"><a href="#应用级数据" class="headerlink" title="应用级数据"></a>应用级数据</h4><p>在 <code>src/app.tsx</code> 中可通过 <code>getInitialData</code> 获取全局数据：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; runApp &#125; <span class="keyword">from</span> <span class="string">'rax-app'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> appConfig = &#123;</span><br><span class="line">+  app: &#123;</span><br><span class="line">+    getInitialData: <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">+      <span class="comment">// const data = await fetch('/api/data');</span></span><br><span class="line">+      <span class="keyword">return</span> &#123; <span class="attr">user</span>: &#123; <span class="attr">name</span>: <span class="string">'Jack Ma'</span>, <span class="attr">id</span>: <span class="string">'01'</span> &#125; &#125;</span><br><span class="line">+    &#125;</span><br><span class="line">+  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">runApp(appConfig);</span><br></pre></td></tr></table></figure>

<p>开启了 SSR 的行为说明：</p>
<ul>
<li>服务端渲染时直接调用 <code>getInitialData</code> 获取数据并渲染应用，同时将数据注入到全局变量中</li>
<li>浏览器端渲染时不再调用 <code>getInitialData</code>，会直接通过全局变量获取初始数据</li>
<li>可以获取到当前请求的上下文 ctx 参数，包含以下字段<ul>
<li>ctx.req：HTTP request 对象 （仅在 server 端输出）</li>
<li>ctx.res：HTTP response 对象 （仅在 server 端输出）</li>
<li>ctx.pathname：当前路由路径</li>
<li>ctx.query：请求参数对象</li>
<li>ctx.path：URL 路径（包括请求参数）</li>
</ul>
</li>
</ul>
<p>未开启 SSR 的行为说明：</p>
<ul>
<li>浏览器端会同步调用 <code>getInitialData</code>，调用完成后执行 render 逻辑</li>
</ul>
<p>数据消费</p>
<p>通过 <code>getInitialData()</code> 获取的全局数据可以通过以下两种方式消费使用：</p>
<ul>
<li>全局 Store 消费：状态管理-设置初始状态</li>
<li>自定义消费：应用入口-getInitialData</li>
</ul>
<h4 id="页面级数据"><a href="#页面级数据" class="headerlink" title="页面级数据"></a>页面级数据</h4><p>在页面级组件中通过 <code>Component.getInitialProps</code> 来获取页面初始数据：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; createElement &#125; <span class="keyword">from</span> <span class="string">'rax'</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Home</span>(<span class="params">&#123; stars &#125;</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>rax stars: &#123;stars&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">+Home.getInitialProps = <span class="keyword">async</span> (ctx) =&gt; &#123;</span><br><span class="line">+  <span class="keyword">const</span> res = <span class="keyword">await</span> fetch(<span class="string">'https://github.com/alibaba/rax'</span>);</span><br><span class="line">+  <span class="keyword">return</span> &#123; <span class="attr">stars</span>: res.data.stargazers_count &#125;;</span><br><span class="line">+&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Home;</span><br></pre></td></tr></table></figure>

<p>开启了 SSR 的行为说明：</p>
<ul>
<li>服务端渲染时调用对应页面的 <code>getInitialProps</code>，然后在渲染页面组件时将数据作为 props 传递给页面组件，同时将数据注入到全局变量上</li>
<li>浏览器端渲染时不再调用 <code>getInitialProps</code>，会直接通过全局变量获取初始数据并作为组件的 props</li>
<li>可以获取到当前请求的上下文 ctx 参数，包含以下字段<ul>
<li>ctx.req：HTTP request 对象 （仅在 server 端输出）</li>
<li>ctx.res：HTTP response 对象 （仅在 server 端输出）</li>
<li>ctx.pathname：当前路由路径</li>
<li>ctx.query：请求参数对象</li>
<li>ctx.path：URL 路径（包括请求参数）</li>
</ul>
</li>
</ul>
<p>未开启 SSR 的行为说明：</p>
<ul>
<li>浏览器端渲染时会在组件渲染前调用该方法，同时作为 props 传递给页面组件。</li>
</ul>

    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/11/22/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/RaxWEB%E5%BA%94%E7%94%A8/" title="RaxWEB应用">http://example.com/2021/11/22/盒马技术栈初学/RaxWEB应用/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%85%AC%E5%8F%B8%E6%8A%80%E6%9C%AF%E6%A0%88/" rel="tag"># 公司技术栈</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/11/20/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/Rax%E5%88%9D%E5%AD%A6/" rel="prev" title="Rax初学">
      <i class="fa fa-chevron-left"></i> Rax初学
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/11/24/%E7%9B%92%E9%A9%AC%E6%8A%80%E6%9C%AF%E6%A0%88%E5%88%9D%E5%AD%A6/Rax%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%BA%94%E7%94%A8/" rel="next" title="Rax小程序应用">
      Rax小程序应用 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#WEB应用"><span class="nav-number">1.</span> <span class="nav-text">WEB应用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#多页应用"><span class="nav-number">1.1.</span> <span class="nav-text">多页应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#应用配置"><span class="nav-number">1.1.1.</span> <span class="nav-text">应用配置</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#开启-MPA"><span class="nav-number">1.1.1.1.</span> <span class="nav-text">开启 MPA</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#配置页面"><span class="nav-number">1.1.1.2.</span> <span class="nav-text">配置页面</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#window"><span class="nav-number">1.1.1.3.</span> <span class="nav-text">window</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tabBar"><span class="nav-number">1.1.1.4.</span> <span class="nav-text">tabBar</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#调试阶段编译指定页面"><span class="nav-number">1.1.1.5.</span> <span class="nav-text">调试阶段编译指定页面</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#页面入口"><span class="nav-number">1.1.2.</span> <span class="nav-text">页面入口</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#导出组件"><span class="nav-number">1.1.2.1.</span> <span class="nav-text">导出组件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#自定义渲染"><span class="nav-number">1.1.2.2.</span> <span class="nav-text">自定义渲染</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#runApp"><span class="nav-number">1.1.2.3.</span> <span class="nav-text">runApp</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#定制-HTML-结构"><span class="nav-number">1.1.3.</span> <span class="nav-text">定制 HTML 结构</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#默认-HTML-内容"><span class="nav-number">1.1.3.1.</span> <span class="nav-text">默认 HTML 内容</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#通过-app-json-定制"><span class="nav-number">1.1.3.2.</span> <span class="nav-text">通过 app.json 定制</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#通过-Document-定制"><span class="nav-number">1.1.3.3.</span> <span class="nav-text">通过 Document 定制</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#状态管理"><span class="nav-number">1.1.4.</span> <span class="nav-text">状态管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#使用指南"><span class="nav-number">1.1.4.1.</span> <span class="nav-text">使用指南</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#目录结构"><span class="nav-number">1.1.4.1.1.</span> <span class="nav-text">目录结构</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#定义模型状态"><span class="nav-number">1.1.4.1.2.</span> <span class="nav-text">定义模型状态</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#创建-Store-实例"><span class="nav-number">1.1.4.1.3.</span> <span class="nav-text">创建 Store 实例</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#View-中使用模型状态"><span class="nav-number">1.1.4.1.4.</span> <span class="nav-text">View 中使用模型状态</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模型规范"><span class="nav-number">1.1.4.2.</span> <span class="nav-text">模型规范</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#模型定义"><span class="nav-number">1.1.4.2.1.</span> <span class="nav-text">模型定义</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#state"><span class="nav-number">1.1.4.2.2.</span> <span class="nav-text">state</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#reducers"><span class="nav-number">1.1.4.2.3.</span> <span class="nav-text">reducers</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#effects"><span class="nav-number">1.1.4.2.4.</span> <span class="nav-text">effects</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#模型使用"><span class="nav-number">1.1.4.2.5.</span> <span class="nav-text">模型使用</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#API"><span class="nav-number">1.1.4.3.</span> <span class="nav-text">API</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#类型提示"><span class="nav-number">1.1.4.4.</span> <span class="nav-text">类型提示</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#创建-Store-实例时"><span class="nav-number">1.1.4.4.1.</span> <span class="nav-text">创建 Store 实例时</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#定义-Model-时"><span class="nav-number">1.1.4.4.2.</span> <span class="nav-text">定义 Model 时</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#静态构建"><span class="nav-number">1.1.5.</span> <span class="nav-text">静态构建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#快照"><span class="nav-number">1.1.6.</span> <span class="nav-text">快照</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#单页应用"><span class="nav-number">1.2.</span> <span class="nav-text">单页应用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#应用入口"><span class="nav-number">1.2.1.</span> <span class="nav-text">应用入口</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#配置项"><span class="nav-number">1.2.1.1.</span> <span class="nav-text">配置项</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#路由方案"><span class="nav-number">1.2.2.</span> <span class="nav-text">路由方案</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#路由配置"><span class="nav-number">1.2.2.1.</span> <span class="nav-text">路由配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#页面路由"><span class="nav-number">1.2.2.2.</span> <span class="nav-text">页面路由</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#window-1"><span class="nav-number">1.2.2.3.</span> <span class="nav-text">window</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tabBar-1"><span class="nav-number">1.2.2.4.</span> <span class="nav-text">tabBar</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#路由跳转"><span class="nav-number">1.2.2.5.</span> <span class="nav-text">路由跳转</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#路由参数获取"><span class="nav-number">1.2.2.6.</span> <span class="nav-text">路由参数获取</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#常见问题"><span class="nav-number">1.2.2.7.</span> <span class="nav-text">常见问题</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#定制-HTML-结构-1"><span class="nav-number">1.2.3.</span> <span class="nav-text">定制 HTML 结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#状态管理-1"><span class="nav-number">1.2.4.</span> <span class="nav-text">状态管理</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#全局状态"><span class="nav-number">1.2.4.1.</span> <span class="nav-text">全局状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#定义模型"><span class="nav-number">1.2.4.2.</span> <span class="nav-text">定义模型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建-Store-实例-1"><span class="nav-number">1.2.4.3.</span> <span class="nav-text">创建 Store 实例</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#在-View-中使用状态"><span class="nav-number">1.2.4.4.</span> <span class="nav-text">在 View 中使用状态</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模型规范-1"><span class="nav-number">1.2.4.5.</span> <span class="nav-text">模型规范</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#API-1"><span class="nav-number">1.2.4.6.</span> <span class="nav-text">API</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#类型提示-1"><span class="nav-number">1.2.4.7.</span> <span class="nav-text">类型提示</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#设置初始状态"><span class="nav-number">1.2.4.8.</span> <span class="nav-text">设置初始状态</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#生命周期"><span class="nav-number">1.2.5.</span> <span class="nav-text">生命周期</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#应用生命周期"><span class="nav-number">1.2.5.1.</span> <span class="nav-text">应用生命周期</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#页面生命周期"><span class="nav-number">1.2.5.2.</span> <span class="nav-text">页面生命周期</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Hooks"><span class="nav-number">1.2.5.3.</span> <span class="nav-text">Hooks</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#触发时序"><span class="nav-number">1.2.5.4.</span> <span class="nav-text">触发时序</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开启-SSR"><span class="nav-number">1.2.6.</span> <span class="nav-text">开启 SSR</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#应用级数据"><span class="nav-number">1.2.6.1.</span> <span class="nav-text">应用级数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#页面级数据"><span class="nav-number">1.2.6.2.</span> <span class="nav-text">页面级数据</span></a></li></ol></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

